package com.cxf.windex.rest;

import com.cxf.windex.base.msg.ObjectRestResponse;
import com.cxf.windex.base.msg.TableResultResponse;
import com.cxf.windex.base.rest.BaseRestController;
import com.cxf.windex.base.util.IdUtil;
import com.cxf.windex.biz.JsContentsBiz;
import com.cxf.windex.entity.JsContents;
import com.cxf.windex.entity.JsNotes;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import groovy.util.logging.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author chenxf
 * @date 3/8/2018
 * @description
 */
@Api(tags = {"通用 - 资讯接口"})
@RestController
@RequestMapping("/user/content")
public class ContentController extends
        BaseRestController<JsContentsBiz, JsContents> {


  public static Pattern p = Pattern.compile("<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>");

  /**
   * 文件存放 路径
   */
  @Value("${upload.path}")
  private String uploadPath;

  /**
   * 文件访问路径
   */
  @Value("${upload.url}")
  private String uploadUrl;

  /**
   * 文件访问域名
   */
  @Value("${upload.uri}")
  private String uploadUri;



  @ApiOperation(value = "查询文章列表", notes = "查询文章列表")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "limit", value = "记录数", paramType = "query", dataType = "int"),
      @ApiImplicitParam(name = "offset", value = "页码",  paramType = "query", dataType = "int"),
      @ApiImplicitParam(name = "orderStr", value = "排序 desc",  paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = "title", value = "标题",  paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = "range", value = "用户类型 0商家 1村长",  paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = "type", value = "文章类型",  paramType = "query", dataType = "String")
  })
  @GetMapping("/list")
  public TableResultResponse<JsContents> list(
          @RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1") int offset,
          @RequestParam(defaultValue = "desc") String orderStr, String title, String type,String range) {
    List<JsContents> items = baseBiz.list(limit,offset,orderStr,title,type,range);
    PageInfo<JsContents> pageInfo = new PageInfo<>(items);
    return new TableResultResponse<>(Integer.parseInt(String.valueOf(pageInfo.getTotal())),
            offset, limit,
            items);


  }

  @ApiOperation(value = "保存", notes = "保存")
  @PostMapping( "/save")
  public ObjectRestResponse save( JsContents entity) {
    baseBiz.save(entity);
    return new ObjectRestResponse().rel(true).msg("保存成功");
  }


  @DeleteMapping("/del/{id}")
  public ObjectRestResponse<JsContents> remove(@PathVariable String id){
    baseBiz.deleteById(id);
    return new ObjectRestResponse().rel(true);
  }

}
